AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include("shared.lua")

function ENT:PostEntityPaste( ply, ent, createdents )
	self.ply = ply
	ply.turrets = ply.turrets+1
	local modelname = self.Entity:GetModel()
	if( modelname=="models/hunter/blocks/cube1x1x05.mdl") then
		self.price = 0
	elseif( modelname=="models/hunter/blocks/cube2x2x05.mdl") then
		self.price = 400
	elseif( modelname=="models/hunter/blocks/cube3x3x05.mdl") then
		self.price = 800
	else
		self.price = 400
	end
	
	if(tonumber(self.ply.money) < self.price or ply.turrets>6) then 
	self.Repay = false
	self.Entity:Remove()
	return false
	else
	self.ply.money = self.ply.money-self.price
	MoneyHUDUpdate( self.ply )
	SavePlayerData( self.ply )
	end
end

function ENT:Initialize()
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetTrigger( true )
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetUseType(SIMPLE_USE)
	self.Entity:SetMaterial("models/lk_materials/metalpipe009a")
	
	local phy = self.Entity:GetPhysicsObject()
	if phy:IsValid() then phy:Wake() end

	local modelname = self.Entity:GetModel()
	if( modelname=="models/hunter/blocks/cube1x1x05.mdl") then
		self.price = 0
	elseif( modelname=="models/hunter/blocks/cube2x2x05.mdl") then
		self.price = 400
	elseif( modelname=="models/hunter/blocks/cube3x3x05.mdl") then
		self.price = 800
	else
		self.price = 400
	end
	
	if( modelname=="models/hunter/blocks/cube1x1x05.mdl") then
		self.turret = ents.Create( "lk_lightturret" )
	elseif( modelname=="models/hunter/blocks/cube2x2x05.mdl") then
		self.turret = ents.Create( "lk_medturret" )
	elseif( modelname=="models/hunter/blocks/cube3x3x05.mdl") then
		self.turret = ents.Create( "lk_monitor" )
	else
		self.turret = ents.Create( "lk_medturret" )
	end
	
		self.turret:SetPos( self.Entity:GetPos() + self.Entity:GetUp()*12 )
		self.turret:SetAngles( self.Entity:GetAngles() )
		self.turret:Activate()
		self.turret:Spawn()
		self.turret:SetParent( self.Entity )
		self.turret:SetOwner( self.Entity:GetOwner() )
		self.turret.base = self.Entity
	
	self.Repay = true
	
	self.maxhp = GetHealth( self.Entity )
	self.hp = self.maxhp
	
	self.core = NULL
	
	self.gunyaw = 0
	self.gunpitch = 0
	
	self.Entity:NextThink( CurTime() )
end

function ENT:Think()

	if(!self.core:IsValid()) then return end
	if(!self.turret:IsValid()) then return end
	
	local pos = self.turret:WorldToLocal( self.Entity:GetPos() + self.Entity:GetForward()*100 + self.Entity:GetUp()*12 )
	if( self.core!=nil and self.core:GetClass()=="lk_core" and self.core.aimPos!=Vector(0,0,0)) then
		pos = self.turret:WorldToLocal( self.core.aimPos )
	end
	
	local rad2deg = 180 / math.pi
		local bearing = rad2deg*-math.atan2(pos.y, pos.x)
		if( math.abs(bearing)>self.turret.aimspeed ) then
		bearing = bearing/math.abs(bearing)
		self.gunyaw = self.gunyaw - bearing*self.turret.aimspeed
		else
		self.gunyaw = self.gunyaw - bearing
		end
		
		local len = pos:Length()
		local elevation = rad2deg*math.asin(pos.z / len)
		local pitchdelta = self.gunpitch-elevation
		if( math.abs(pitchdelta)>self.turret.aimspeed ) then
		pitchdelta = pitchdelta/math.abs(pitchdelta)
		self.gunpitch = self.gunpitch - pitchdelta*self.turret.aimspeed
		else
		self.gunpitch = elevation
		end
		
	self.turret:SetAngles( self.Entity:LocalToWorldAngles(Angle( 0,self.gunyaw,0)) )
	
	self.Entity:NextThink( CurTime() + 0.02 )
	return true

end

function ENT:OnRemove()
	if(self.Repay==true) then
		if(self.core and self.core:IsValid()) then mult = self.core.hp/self.core.maxhp else mult = self.hp/self.maxhp end
		self.ply.money = self.ply.money + self.price*mult
		MoneyHUDUpdate( self.ply )
		SavePlayerData( self.ply )
	end
	self.ply.turrets = self.ply.turrets-1
end